package com.alphaking.controller.admin;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.alphaking.service.mingweicai.CategoryService;

@Controller
public class CategoryController {
	
	@Resource
    private CategoryService productManageService ;

	public CategoryService getProductManageService() {
		return productManageService;
	}

	public void setProductManageService(CategoryService productManageService) {
		this.productManageService = productManageService;
	} 
	
	/**
	 * 跳转到分类的页面
	 * @return
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 */
	@RequestMapping("/admin/toProductCategoryPage")
	public String toProductCategoryPage(Model model) throws NoSuchFieldException, SecurityException{
	
		//查询数据
		this.productManageService.findCategorys(model);
		
		return "/admin/category" ; 
	}
    
	/**
	 * 添加分类
	 * @return
	 */
	@RequestMapping("/admin/addcategory")
    public String addCatogery(String productCategoryName, String productCategoryUrl){
   	
    	this.getProductManageService().addCategory(productCategoryName , productCategoryUrl);

    	return "redirect:/admin/toProductCategoryPage";
    }
	
	/**
	 * 更新分类
	 * @param jsonCondition
	 * @param categoryJson
	 * @param arr
	 * @return
	 */
	@RequestMapping("/admin/updateCategory")
	public String updateCategory(String productCategoryId, String productCategoryName, String productCategoryUrl,RedirectAttributes arr){

		this.getProductManageService().updateCategory(productCategoryId,productCategoryName,productCategoryUrl);
    	return "redirect:/admin/toProductCategoryPage";
	}
    

	/**
	 * 删除分类
	 * @param id
	 * @return
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 */
	@RequestMapping("/admin/hideOrshowCategory")
	public String hideOrdisplayCategory(String id) throws NoSuchFieldException, SecurityException{

		this.getProductManageService().hideOrShowFirstCategory(id);
    	return "redirect:/admin/toProductCategoryPage";		
	}
	
	/**
	 * 修改顺序
	 * @param firstId
	 * @param secondId
	 * @return
	 */
	@RequestMapping("/admin/changeOrder")
	public String changeOrder(String firstId , String secondId){
		
		this.getProductManageService().changeOrder(firstId, secondId);
    	return "redirect:/admin/toProductCategoryPage";		
	}
	
	/**
	 * 删除分类
	 * @return
	 * @throws SecurityException 
	 * @throws NoSuchFieldException 
	 */
	@RequestMapping("/admin/deleteCategory")
	public String deleteCategory(String id) throws NoSuchFieldException, SecurityException{
		
		this.getProductManageService().deleteFirstCategory(id);
		return "redirect:/admin/toProductCategoryPage";		
	}
	
}
